package ops.model.manager;

import java.io.File;
import java.util.Map;
import java.util.Map.Entry;

import ops.model.Class;
import ops.model.Subject;

public class SubjectManager extends EntityManager<Subject> {

	private static final File SUBJECTS_FILE = new File("data/subjects.dat");
	
	public SubjectManager() {
		super();
	}
	
	@Override
	public File getFile() {
		return SUBJECTS_FILE;
	}

	@Override
	protected Object getKeyForEntity(Subject element) {
		return element.getCode();
	}
	
	@Override
	public Subject remove(Object key) {
		//removing classes that references this subject.
		ClassManager classManager = EntityController.getInstance().getClassManager();
		Map<Object, Class> classes = classManager.getModifiableMap();
		for (Entry<Object, Class> entry : classes.entrySet()) {
			Class clazz = entry.getValue();
			if (clazz.getSubject() == (Integer) key) {
				classManager.remove(key);
			}
		}
		
		return super.remove(key);
	}

}
